<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
require(dirname(dirname(__FILE__)) . '/includes/application_top.php');

//
include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'core.directory.class.php');

//
function _check_encode($dir, $convert = 0) {
	$dObj = new CoreDirectoryParser(CONFIG_PATH . $dir);
	$dObj->parse();
	$_dir = $dObj->_dir;
	$dirs = $dObj->getDirs();
	$files = $dObj->getFiles();
	//
	if( is_array($files) ) {
		//
		foreach($files as $key => $file) {
			$c = file_get_contents($_dir . $file['file']);
			$dc = mb_detect_encoding($c);echo '<br>';
			echo $dc;
			echo ', ';
			echo $file['file'];
			echo ', ';
			//if( $dc == 'ASCII' && $convert == 1 ) {
			if( $convert == 1 ) {
				$from = 'GB2312';
				$to = 'UTF-8';
				//
				//$c = iconv($from, $to, $c);
				$c = mb_convert_encoding($c, $to);
				//
				$dc = mb_detect_encoding($c);
				echo $dc;
				echo ', ';
				echo 'convert';
				echo ', ';
				echo _check_isutf8($c);
				//echo file_put_contents($_dir . $file['file'], $c);
			}
			echo '<br>';
		}
		//
		foreach($dirs as $key => $value) {
			_check_encode($dir . $value . '/', $convert);
		}
	}
}

//
function _check_isutf8($c) {
	$cc = mb_convert_encoding($c, 'UTF-32', 'UTF-8');
	$cc = mb_convert_encoding($cc, 'UTF-8', 'UTF-32');
	if( $c == $cc ) {
		return true;	
	}
	
	return false;
}

//
$dir = 'templates/default_zh/admin/';

//
_check_encode($dir, 1);

//
echo 'OK';
//
?>